﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SolveCranks.CApSCSolveCranksApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.SolveCranks.Resources;
using EngineeringInternalExtension;
using EngineeringInternalExtension.ModelObjects;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.SolveCranks;

public class CApSCSolveCranksApp
{
  private static TxTypeFilter _supportedObjsFilter = new TxTypeFilter(typeof (ITxKinematicsModellable));

  public void ExecuteWithUI(TxObjectList targetObjects)
  {
    if (!this.VerifyComponentsAreNotOpenForModeling(targetObjects))
      return;
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    bool targetObjectsSelectedOnly = this.ContainSameObjects(targetObjects, allItems);
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    CApSCSolveCranksNotificationHandler notificationHandler = (CApSCSolveCranksNotificationHandler) null;
    bool flag = false;
    try
    {
      flag = TxApplicationEx.FreezeDisplay();
      List<CApSCSolveCranksAlgorithm.Result> algorithmResults = this.Execute(targetObjects);
      if (algorithmResults.Count > 0)
      {
        notificationHandler = new CApSCSolveCranksNotificationHandler(algorithmResults, targetObjectsSelectedOnly);
        notificationHandler.SaveLog();
        notificationHandler.PrepareNotification();
        if (flag)
        {
          TxApplicationEx.UnfreezeDisplay();
          flag = false;
        }
        TxApplication.RefreshDisplay();
      }
    }
    finally
    {
      if (flag)
        TxApplicationEx.UnfreezeDisplay();
      TxApplication.ActiveSelection.SetItems(this.GetExistingObjects(allItems));
      Cursor.Current = current;
    }
    if (notificationHandler == null)
      return;
    this.ShowResults(notificationHandler);
    notificationHandler.Clear();
  }

  public List<CApSCSolveCranksAlgorithm.Result> Execute(TxObjectList targetObjects)
  {
    List<CApSCSolveCranksAlgorithm.Result> resultList = new List<CApSCSolveCranksAlgorithm.Result>(((Collection<ITxObject>) targetObjects).Count);
    foreach (ITxObject targetObject in (Collection<ITxObject>) targetObjects)
    {
      CApSCSolveCranksAlgorithm.Result result = this.Execute(targetObject);
      if (result != null)
        resultList.Add(result);
    }
    return resultList;
  }

  public CApSCSolveCranksAlgorithm.Result Execute(ITxObject targetObject)
  {
    return targetObject is ITxKinematicsModellable ? new CApSCSolveCranksAlgorithm(targetObject as ITxKinematicsModellable).Execute() : (CApSCSolveCranksAlgorithm.Result) null;
  }

  public void ShowResults(
    CApSCSolveCranksNotificationHandler notificationHandler)
  {
    notificationHandler.Notify();
  }

  public static bool IsSupported(ITxKinematicsModellable modelComp)
  {
    return ITxKinematicsModellableEx.CanSolveCranks(modelComp);
  }

  public static bool IsAllowedParentForSupportedObjects(ITxObject obj)
  {
    if (!(obj is ITxObjectCollection))
      return false;
    if (obj is TxCompoundResource)
      return true;
    return obj is ITxComponent && (obj as ITxComponent).IsEquipment;
  }

  public static bool ContainsSupportedObjects(ITxObject obj)
  {
    if (CApSCSolveCranksApp.IsAllowedParentForSupportedObjects(obj))
    {
      foreach (ITxKinematicsModellable allDescendant in (Collection<ITxObject>) (obj as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) CApSCSolveCranksApp._supportedObjsFilter))
      {
        if (CApSCSolveCranksApp.IsSupported(allDescendant))
          return true;
      }
    }
    return false;
  }

  public static TxObjectList ExtractSupportedObjects(ITxObject obj)
  {
    TxObjectList supportedObjects = new TxObjectList();
    if (CApSCSolveCranksApp.IsAllowedParentForSupportedObjects(obj))
    {
      foreach (ITxKinematicsModellable allDescendant in (Collection<ITxObject>) (obj as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) CApSCSolveCranksApp._supportedObjsFilter))
      {
        if (CApSCSolveCranksApp.IsSupported(allDescendant))
          ((Collection<ITxObject>) supportedObjects).Add((ITxObject) allDescendant);
      }
    }
    return supportedObjects;
  }

  public static TxObjectList ExtractUniqueSupportedObjects(TxObjectList objs)
  {
    TxObjectList supportedObjects = new TxObjectList();
    foreach (ITxObject modelComp in (Collection<ITxObject>) objs)
    {
      if (modelComp is ITxKinematicsModellable && CApSCSolveCranksApp.IsSupported(modelComp as ITxKinematicsModellable) && !((Collection<ITxObject>) supportedObjects).Contains(modelComp))
        ((Collection<ITxObject>) supportedObjects).Add(modelComp);
      foreach (ITxObject supportedObject in (Collection<ITxObject>) CApSCSolveCranksApp.ExtractSupportedObjects(modelComp))
      {
        if (!((Collection<ITxObject>) supportedObjects).Contains(supportedObject))
          ((Collection<ITxObject>) supportedObjects).Add(supportedObject);
      }
    }
    return supportedObjects;
  }

  private bool VerifyComponentsAreNotOpenForModeling(TxObjectList targetObjects)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxObject targetObject in (Collection<ITxObject>) targetObjects)
    {
      if (this.IsOpenForModeling(targetObject))
        ((Collection<ITxObject>) txObjectList).Add(targetObject);
    }
    if (((Collection<ITxObject>) txObjectList).Count <= 0)
      return true;
    int num = (int) TxMessageBox.Show(((Collection<ITxObject>) txObjectList).Count != 1 ? CApSCSolveCranksResources.WraningMsgMultipleComponentsOpenForModeling : string.Format(CApSCSolveCranksResources.WarningMsgOpenForModeling, (object) ((Collection<ITxObject>) txObjectList)[0].Name), CApSCSolveCranksResources.CommandName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    return false;
  }

  private bool IsOpenForModeling(ITxObject obj)
  {
    bool flag = false;
    if (obj is ITxComponent && (obj as ITxComponent).IsOpenForModeling)
      flag = true;
    return flag;
  }

  private TxObjectList GetExistingObjects(TxObjectList objs)
  {
    TxObjectList existingObjects = new TxObjectList();
    foreach (ITxObject itxObject in (Collection<ITxObject>) objs)
    {
      if (itxObject.IsValid())
        ((Collection<ITxObject>) existingObjects).Add(itxObject);
    }
    return existingObjects;
  }

  private bool ContainSameObjects(TxObjectList objs1, TxObjectList objs2)
  {
    if (((Collection<ITxObject>) objs1).Count != ((Collection<ITxObject>) objs2).Count)
      return false;
    foreach (ITxObject itxObject in (Collection<ITxObject>) objs1)
    {
      if (!((Collection<ITxObject>) objs2).Contains(itxObject))
        return false;
    }
    return true;
  }
}
